



import re
import json

HEADERS = {}


def header2Json(source='./headers.txt', dest=None):
    if not dest:
        dest = '{}.json'.format(re.findall(r'(\.\/)?(\w+).txt', source)[0][-1])

    with open(source, 'r') as f:
        for chuck in f:
            if ':' in chuck:
                data = re.findall(r'(.+?):\s?(.*|)\n?', chuck)
                try:
                    data = data[0]
                    HEADERS[data[0]] = data[1]
                except(Exception):
                    pass
    with open(dest, 'w') as f:
        f.write(json.dumps(HEADERS))


if __name__=='__main__':
    header2Json('headers.txt')  # 进行数据转换


